define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'paotui/post/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    import_url: '',
                    table: 'paotui_post',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns:true,
                fixedNumber:2,
                fixedRightNumber:2,
                columns: [
                    [
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), searchList: {"-1": __('已取消'),"1": __('未支付'), "2": '抢单中', "3": '待揽收',"4": '运输中',"6": '已送达'},formatter:Controller.api.formatter.status},
                        // {field: 'user_id', title: __('用户ID'), operate: '='},
                        {field: 'poster_name', title: __('骑手信息'),align:'left', operate:false},
                        // {field: 'type', title: __('订单类型'),operate:false,formatter:Controller.api.formatter.type},
                        {field: 'is_appoints', title: __('取件时间'),align: 'left',operate:false,formatter:Controller.api.formatter.is_appoints},
                        {field: 'from_area', title: __('发件'),align: 'left',operate:false, formatter:Controller.api.formatter.from_area},
                        {field: 'to_area', title: __('收件'),align: 'left', operate:false,formatter:Controller.api.formatter.to_area},
                        {field: 'from_name', title: __('发件人姓名'), operate: 'LIKE',visible:false},
                        {field: 'from_mobile', title: __('发件人手机号'), operate: 'LIKE',visible:false},
                        {field: 'from_district', title: __('发件县/区'), operate: 'LIKE',visible:false},
                        {field: 'from_poi', title: __('From_poi'), operate: false,visible:false},
                        {field: 'from_address', title: __('From_address'), operate: 'LIKE',visible:false},
                        {field: 'to_name', title: __('收件人姓名'), operate: 'LIKE',visible:false},
                        {field: 'to_mobile', title: __('收件人手机号'), operate: 'LIKE',visible:false},
                        {field: 'to_district', title: __('收件县/区'), operate: 'LIKE',visible:false},
                        {field: 'to_poi', title: __('To_poi'), operate: false,visible:false},
                        {field: 'to_address', title: __('To_address'), operate: 'LIKE',visible:false},
                        {field: 'distance', title: __('配送距离（km）'), operate:'BETWEEN'},
                        {field: 'total_price', title: __('订单金额'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('实际支付金额'), operate:'BETWEEN'},
                        {field: 'brokerage', title: __('佣金'), operate: false},
                        // {field: 'poster_type', title: __('佣金类型'), operate: false},
                        {field: 'goods_type', title: __('物品类型'), operate: 'LIKE',formatter:function (value,row,index){ return row.goods_type+"/"+row.weight+"kg"}},
                        {field: 'images', title: __('取件照片'), operate: false, events: Table.api.events.image,formatter:Controller.api.formatter.images},
                        {field: 'reach_images', title: __('送达照片'), operate: false, events: Table.api.events.image,formatter:Controller.api.formatter.images},
                        {field: 'createtime', title: __('下单时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'poster_id', title: __('快递员'),visible:false,searchList: $.getJSON("paotui/poster/dynamicselect")},
                        {field: 'transaction_id', title: __('微信订单号'), operate: 'LIKE'},
                        {field: 'note', title: __('备注'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'),align:'left', table: table, events: Table.api.events.operate,
                            buttons: [{
                                visible: function (row) {
                                    return row.status == 3 || row.status == 4 || row.status == 5;
                                },
                                name: 'order',
                                text: __('日志'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'paotui/post/order'
                            },
                            {
                                visible: function (row) {
                                    return row.status == 2 || row.status == 8 || row.status == 9;
                                },
                                name: 'hand_dispatch',
                                text: __('人工指派'),

                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                url: 'paotui/post/hand_dispatch'
                            },
                            {
                                visible: function (row) {
                                    return row.status == 3 || row.status == 4;
                                },
                                name: 'change_poster',
                                text: __('更换骑手'),

                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                url: 'paotui/post/change_poster'
                            }],
                            formatter: Table.api.formatter.operate},
                    ]
                ]
            });


            $(".nav-tabs a").click(function (){
                table.bootstrapTable('refresh', {url:$.fn.bootstrapTable.defaults.extend.index_url+'&type='+$(this).data('value')});
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        change_poster: function () {
            Controller.api.bindevent();
        },
        hand_dispatch: function () {
            Controller.api.bindevent();
        },
        third_order: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        order: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'paotui/post/order?id='+$("#table").attr('data-id'),
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder:'desc',
                search:false,
                commonSearch: false,
                pagination:false,
                columns: [
                    [
                        {
                            title:'序号',
                            operate:false,
                            sortable:false,
                            formatter:function(value,row,index){
                                return index+1;
                            }
                        },
                        {field: 'type_name', title: __('操作')},
                        {field: 'poster_name', title: __('快递员')},
                        {field: 'poster_mobile', title: __('快递员手机号')},
                        {field: 'createtime', title: __('时间')},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {

            formatter:{
                status: function (value, row, index) {
                    switch (value) {
                        case -1:
                            return "已取消";
                        case 1:
                            return "待支付";
                        case 2:
                            return "<span style='color: #ff0000'>抢单中</span>";
                        case 3:
                            return "待揽收";
                        case 4:
                            return "运输中";
                        case 5:
                            return "<span style='color: #05ab0c'>已送达</span>";
                        case 8:
                            return "<span style='color: #1e0eff'>人工指派</span>";
                        case 9:
                            return "<span style='color: #1eaaff'>系统指派</span>";
                        default:
                            return value;
                    }
                },
                is_appoints:function (value, row, index) {
                    switch (value) {
                        case 0:
                            return "立即取件";
                        case 1:
                            return row.appoints_day+" "+row.appoints_time;
                    }
                },
                images:function (value, row, index) {
                    var images_html = "";
                    if(value){

                        var _images = value.split(",");
                        _images.forEach(function (item) {
                            images_html += '<a href="javascript:" style="margin-left: 5px;"><img class="img-sm img-center" src="'+item+'"></a>'
                        })
                    }
                    return images_html;
                },
                from_area:function (value, row, index) {
                    return row.from_district+" "+row.from_poi+" "+row.from_address+"<br/>"+row.from_name+" "+row.from_mobile;
                },
                to_area:function (value, row, index) {
                    if(row.type == 3){
                        return row.to_province+row.to_city+row.to_district+" "+row.to_poi+" "+row.to_address+"<br/>"+row.to_name+" "+row.to_mobile;
                    }else{
                        return row.to_district+" "+row.to_poi+" "+row.to_address+"<br/>"+row.to_name+" "+row.to_mobile;
                    }

                },
                type: function (value, row, index) {
                    switch (value) {
                        case 1:
                            return "<span style='color: #05ab0c'>帮我取/帮我送</span>";
                    }
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});